<?php
require APPPATH."/controllers/Common.php";

class HaoPay extends Common {
    private function weixin_pay_res($openid=0,$codeno = 0,$fee=0){
        //使用统一支付接口
        $unifiedOrder = new \UnifiedOrder_pub();
        $unifiedOrder->setParameter("openid", "$openid");//商品描述
        $unifiedOrder->setParameter("body", "订单付款");//商品描述
        //自定义订单号，此处仅作举例
        $unifiedOrder->setParameter("out_trade_no", $codeno);//商户订单号
        $unifiedOrder->setParameter("total_fee", $fee);//总金额
        $unifiedOrder->setParameter("notify_url", "返回地址");//通知地址
        $unifiedOrder->setParameter("trade_type", "JSAPI");//交易类型

        $prepay_id = $unifiedOrder->getPrepayId();
        return $prepay_id;
    }
    public function weixin_hao_pay($codeno = 0,$code=0,$fee=0)
    {

        define("WEIXIN_APPID", HAO_WEIXIN_APPID);
        define("WEIXIN_APPSECRET", HAO_WEIXIN_APPSECRET);
        define("WEIXIN_MCHID", HAO_WEIXIN_MCHID);
        define("WEIXIN_KEY", HAO_WEIXIN_KEY);
        $this->common_vendor('WxPayPubHelper.WxPaypubconfig');
        $this->common_vendor('WxPayPubHelper.SDKRuntimeException');
        $this->common_vendor('WxPayPubHelper.WxPayPubHelper');
		$codeno = $this->req('codeno');
        $fee = $this->req('fee');
        if (!empty($codeno) && $fee) {
            $this->input->set_cookie("codeno",$codeno,60);
            $this->input->set_cookie("fee",$fee,60);
        }
		$msg = $this->req('msg');
		//微信登录返回code
		$code = $this->req('code');


        //使用jsapi接口
        $jsApi = new \JsApi_pub();
        //通过code获得openid
        $openid = '';
        define("JS_API_CALL_URL", "配置返回地址");
        if (!isset($_GET['code'])) {
            //触发微信返回code码
            $url = $jsApi->createOauthUrlForCode(JS_API_CALL_URL);
            Header("Location: $url");
        } else {
            //获取code码，以获取openid
            $code = $_GET['code'];
            $jsApi->setCode($code);
            $openid = $jsApi->getOpenId();
            if($openid == false){
                exit("openid！not catch ,");
            }
        }
        $pay_cash = $fee;
        $fee = $this->input->cookie("fee");
        $fee = $fee * 100;
        $codeno = $this->input->cookie("codeno");
        $prepay_id = $this->weixin_pay_res($openid,$codeno,$fee);

        //=========步骤3：使用jsapi调起支付============
        $jsApi->setPrepayId($prepay_id);
        $jsApiParameters = $jsApi->getParameters();

        $this->parser->parse("weixin_hao_pay",[
            "catename"=>"微信支付",
            "pay_cash"=>$pay_cash,
            "jsApiParameters"=>$jsApiParameters,
        ]);

        $this->resp_html("weixin_hao_pay",[
            "catename"=>"微信支付",
            "pay_cash"=>11,
            "jsApiParameters"=>11,
        ]);
    }

	
}
